﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Wicresoft.EKA.VCenter
{
    public class VCenterConnection
    {
        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly object lockObject = new object();
        private static readonly object lockObjectAppUtil = new object();
        private static readonly object lockObjectExtendUtil = new object();

        public int SleepTime;

        private AppUtil _appUtil = null;
        private ExtendedAppUtil _extendAppUtil = null;
        private VCenterConnection _connection = null;

        private VCenterConnection()
        {
            new Thread(new ThreadStart(this.SessionKeeper)).Start();
        }

        public VCenterConnection Instance
        {
            get
            {
                lock (lockObject) //类级别的锁定，刚开始的时候_connection 为null，所以不能锁定。
                {
                    if (null == _connection)
                        _connection = new VCenterConnection();
                    return _connection;
                }
            }
        }

        public AppUtil GetAppUtil(OptionSpec[] array, string applicationName = "AppUtil")
        {
            lock (lockObjectAppUtil)
            {
                if (null == _appUtil)
                {
                    _appUtil = AppUtil.Initialize(applicationName, array);
                }
                return _appUtil;
            }

        }

        public ExtendedAppUtil GetExtendedAppUtil(OptionSpec[] array, string applicationName = "ExtendedAppUtil")
        {
            lock (lockObjectExtendUtil)
            {
                if (null == _extendAppUtil)
                {
                    _extendAppUtil = ExtendedAppUtil.Initialize(applicationName, array);
                }
                return _extendAppUtil;
            }

        }

        private void SessionKeeper()
        {
            try
            {
                if (null != _appUtil)
                {
                    try
                    {
                        log.Info(_appUtil.ServiceConnection.Service.CurrentTime(_appUtil.ServiceConnection.ServiceInstance).ToString());
                    }
                    catch (Exception e)
                    {
                        log.Error("AppUtil SessionKeeper Error", e);
                        _appUtil.Connect();
                    }
                }

                if (null != _extendAppUtil)
                {

                    try
                    {
                        log.Info(_extendAppUtil.ServiceConnectionV25.Service.CurrentTime(_extendAppUtil.ServiceConnectionV25.ServiceInstance).ToString());
                    }
                    catch (Exception e)
                    {
                        log.Error("ExtendAppUtil SessionKeeper Error", e);
                        _appUtil.Connect();
                    }
                }

            }
            catch (Exception e)
            {
                log.Error("Session Keeper Error", e);
            }
            if (SleepTime != 0)
            {
                Thread.Sleep(SleepTime);
            }
            else
            {
                Thread.Sleep(30 * 60 * 1000);
            }
        }
    }
}
